package cn.itcast.service;

import cn.itcast.dao.MenuMapper;
import cn.itcast.domain.Menu;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;


    /**
     * 根据角色查询菜单
     *
     * @param username
     * @return
     */
    @Override
    public List<Menu> findAllMenus(String username) {
        List<Menu> list = menuMapper.findAllMenus(username);
        //封装menu菜单中的children属性
        List<Menu> listChildren = new ArrayList<>();
        for (Menu menu : list) {
            //获取菜单的id,当成子菜单的parentMenuId;
            Integer id = menu.getId();
            List<Menu> childrenList = menuMapper.findChildrens(id);
            menu.setChildren(childrenList);
            listChildren.add(menu);
        }
        return listChildren;

    }

    /**
     * 新增菜单
     *
     * @paramMenu
     */
    @Override
    public void insertMenu(Menu menu, Integer[] roleIds) {
        menuMapper.insertMenu(menu);
        Integer id = menu.getId();
        this.setRoleAndMenu(id, roleIds);
    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findMenuByPage(QueryPageBean queryPageBean) {

        //健壮性判断
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);

        Page<Menu> page = (Page<Menu>) menuMapper.findMenuByPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除菜单
     *
     * @param id
     */
    @Override
    public void deleteMenu(Integer id) {

        //删除中间表
        menuMapper.deleteRoleMenuId(id);
        //再删除
        menuMapper.deleteMenu(id);

    }

    /**
     * 单个查询菜单
     *
     * @param id
     * @return
     */
    @Override
    public Menu findMenuById(Integer id) {

        return menuMapper.findMenuById(id);
    }

    /**
     * 修改菜单
     *
     * @paramMenu
     */
    @Override
    public void updateMenu(Menu menu, Integer[] roleIds) {


        Integer menuId = menu.getId();
        //删除中间表之后重建
        menuMapper.deleteRoleMenuId(menuId);

        System.out.println("删除成功");
        //修改菜单信息
        menuMapper.updateMenu(menu);
        //重建关系
        this.setRoleAndMenu(menuId, roleIds);

    }

    /**
     * 查询菜单对应的roleIds
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findRoleIds(Integer id) {
        return menuMapper.findRoleIds(id);
    }

    /**
     * 查询所有的菜单信息
     * @return
     */
    @Override
    public List<Menu> findAllMenusByRole() {
        return menuMapper.findAllMenusByRole();
    }

    /*
     * 建立用户和角色的联系
     *
     * @param UserId
     * @param roleId
     */
    public void setRoleAndMenu(Integer menuId, Integer[] roleIds) {
        List<Map<String, Object>> list = new ArrayList();
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("menuId", menuId);
                map.put("roleId", roleId);
                list.add(map);
            }
            //只有当checkItemIds不为空时,才会执行这个方法
            menuMapper.setRoleAndMenu(list);
        }
    }
}
